#include "mainwindow.h"
#include "./ui_mainwindow.h"
#include <QProcess>
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    m_versionNum = "1.0.5";
    m_exeFileName = "Kpy-Pms.exe";
    m_strRemoteUrl ="http://10.35.19.63:8091/autoupdate/autoupdate.json";

    this->dbSqlite = new database();
    this->dbSqlite->initSqlite();
    this->dbSqlite->initCfgData();
    if(this->dbSqlite->vDbSource.size()<2)
    {
        QMessageBox::information(this,"提示","数据源配置有错！");
        return;
    }

    this->dbMySql = new database();
    DbInfo di0 = this->dbSqlite->vDbSource.at(0);
    if(!this->dbMySql->initMysql(di0))
    {
        QMessageBox::information(this,"提示","数据库初始化失败！");
        return;
    }

    this->dbMySql_1 = new database();
    DbInfo di1 = this->dbSqlite->vDbSource.at(1);
    if(!this->dbMySql_1->initMysql(di1))
    {
        QMessageBox::information(this,"提示","数据库初始化失败！");
        return;
    }

    m_taskByPerson = new TaskByPerson(this);
    m_taskByProject = new TaskByProject(this);
//    m_versionByProject = new VersionByProject(this);
    m_taskStoryByVersion = new TaskStoryByVersion(this);
    m_hoursByPerson = new HoursByPerson(this);

    ui->stackedWidget->addWidget(m_taskByProject);
//    ui->stackedWidget->addWidget(m_versionByProject);
    ui->stackedWidget->addWidget(m_taskByPerson);
    ui->stackedWidget->addWidget(m_taskStoryByVersion);
    ui->stackedWidget->addWidget(m_hoursByPerson);

    ui->stackedWidget->setCurrentWidget(m_taskStoryByVersion);

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_menu_taskbyproject_triggered()
{
    qDebug()<<"menu_taskbyproject";
    ui->stackedWidget->setCurrentWidget(m_taskByProject);
}


void MainWindow::on_menu_taskbyperson_triggered()
{
    qDebug()<<"menu_taskbyperson";
    ui->stackedWidget->setCurrentWidget(m_taskByPerson);
}


void MainWindow::on_menu_hourbyperson_triggered()
{
    qDebug()<<"menu_hourbyperson";
    ui->stackedWidget->setCurrentWidget(m_hoursByPerson);
}


void MainWindow::on_menu_versionplan_triggered()
{
    qDebug()<<"menu_versionplan";
    ui->stackedWidget->setCurrentWidget(m_taskStoryByVersion);
}

void MainWindow::on_menu_versionbyproject_triggered()
{
    qDebug()<<"menu_versionplan";
//    ui->stackedWidget->setCurrentWidget(m_versionByProject);
}

void MainWindow::on_menu_checkupdate_triggered()
{
    qDebug()<<"menu_checkupdate"<<m_versionNum;
    QString strExePath = QCoreApplication::applicationDirPath();//获取应用程序可执行文件所在的目录
    QString strExeFile = strExePath +"/"+"autoupdate.exe";

    QStringList args;
    args<<m_versionNum<<m_exeFileName<<m_strRemoteUrl;
    qDebug()<<"启动更新程序:"<<strExeFile<<args;
    QProcess::startDetached(strExeFile,args);
}


void MainWindow::on_menu_exit_triggered()
{
    qDebug()<<"menu_exit";
    QMessageBox::StandardButton result= QMessageBox::question(this,"提示","确定要退出?");
    if(result==QMessageBox::Yes)
        close();

}

void MainWindow::initComboBoxData(QComboBox *cbox, const QString &dataType,const QString &dataCondition)
{
    qDebug()<<"initComboBoxData:"<<dataType<<dataCondition;
    QString sqlStr;
    cbox->clear();
    if(dataType=="项目名称"){

        sqlStr="select zp.name  from zt_project zp where zp.status ='doing' and zp.deleted =1 order by zp.name";
        cbox->addItems(dbMySql->getComboBoxDataBySql(sqlStr));

    }else if(dataType=="迭代版本号"){

        m_prjVerPlan.clear();
        m_prjVerPlan = dbMySql->getProjectVerPlan(dataCondition);
        QStringList sList;
        for(int i=0;i<m_prjVerPlan.size();i++)
        {
            sList<<m_prjVerPlan[i].verPlanName;
        }
        cbox->addItems(sList);

    }else if(dataType=="研发人员"){
        sqlStr="select realname from zt_user where deleted =1 and role not in ('cpo','om') and dept in (72,77,83,82,104,79,78,81,85,86,87,90,91,92,94,93,106) order by realname ";
        cbox->addItems(dbMySql->getComboBoxDataBySql(sqlStr));

    }else if(dataType=="产品名称"){
        sqlStr="select zp.name from zt_product zp where zp.deleted =1 and zp.status = 'normal' order by zp.name";
        cbox->addItems(dbMySql->getComboBoxDataBySql(sqlStr));

    }else {
        qDebug()<< "initComboBoxData is invalid:"<< dataType;
    }
}



